package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

func main() {
	sortTest()
}

func test1() {
	scoreMap := make(map[string]int, 10)
	scoreMap["张三"] = 100
	scoreMap["李四"] = 10
	scoreMap["小明"] = 100
	scoreMap["王五"] = 60

	fmt.Println(scoreMap)

	value, ok := scoreMap["1张三"]
	fmt.Println(value, ok)


	for k, v := range scoreMap{
		fmt.Println(k, v)
	}
	fmt.Println("----------------------")

	delete(scoreMap, "小明")//将小明:100从map中删除
	for k, v := range scoreMap{
		fmt.Println(k, v)
	}


	map1 := make(map[string]interface{}, 10)
	map1["scoreMap"] = scoreMap
	map1["info"] = "成功"
	map1["status"] = 0
	fmt.Println(map1)
}

func sortTest()  {
	rand.Seed(time.Now().UnixNano())

	var scoreMap = make(map[int]string, 10)

	for i := 0; i < 10; i++ {
		key := fmt.Sprintf("stu%02d", i) ////生成stu开头的字符串
		value := rand.Intn(100) ////生成0~99的随机整数
		//scoreMap[key] = value
		scoreMap[value] = key
	}

	//取出map中的所有key存入切片keys
	keys := make([]int, 0, 20)
	for key := range scoreMap{
		keys = append(keys, key)
	}

	//对切片进行排序
	//sort.Strings(keys)
	sort.Ints(keys)

	//按照排序后的key遍历map
	for _, key := range keys {
		fmt.Println(key, scoreMap[key])
	}
}
